/**
 * The TabPanel user-interface component class
 * @class syslib.ui.TabPanel
 * @author Gabriel Ricci
 */
syslib.ui.TabPanel = __class(syslib.ui.VisualTag, 
/** 
 * @lends syslib.ui.TabPanel
 */
{
	id : "", 
	contentBody : "", 
	tabs : "", 
	tabItens : [],
	head : {}, 
	body : {}, 

	prepare : function(element){
		// load the style
		syslib.loadStyleForComponent("tabs");

		// set the class attributes
		this.id = (__valid(this.__attributes.id) ? this.__attributes.id : syslib.element.generateID());
		this.tabs = this.id + "_tabs_container";
		this.contentBody = this.id + "content_container";
		
		// clear the tab itens
		this.tabItens = [];
		
		// return
		return true;
	},

	execute : function(){
		// tag attributes
		var attributes = __extend(this.__attributes, {
			"class" : "syslib-tabpanel-main-container syslib-border", 
			"id" : this.id
		});

		// element structure
		var structure = ["div", attributes,
			["div", {"innerName" : "head", "class" : "syslib-tabpanel-top-container syslib-flat-background syslib-bottom-border", "id" : this.tabs}],
			["div", {"innerName" : "body", "class" : "syslib-tabpanel-content-container", "id" : this.contentBody}, 
				this.__innerHTML
			]
		];

		// create the element
		var tabPanel = this.createDOM(structure);
		delete structure;
		
		// set the data
		this.head = this.__refs.head;
		this.body = this.__refs.body;

		// return
		return tabPanel;
	},

	finalize : function(){
		// register the tabpanel
		syslib.ui.TabPanel.registeredTabPanels[this.id] = this;
	},

	addTab : function(tabItem){
		if(__valid(tabItem) && typeof tabItem === 'object'){
			try{
				// create the index
				var index = this.tabItens.length;
				
				// set the index in the tabitem
				tabItem.index = index;

				// store
				this.tabItens[index] = tabItem;
				
				// add the selector to the head
				this.head.appendChild(tabItem.selector);

				// select, if needed
				if(index == 0){
					tabItem.focusTab();
				}
				
				// return
				return index;
			} catch(e){
				syslib.messages.error("syslib.ui.TabPanel.addTab(): Error adding the tab: " + e.message);
				return false;
			}
		}
	},
	
	removeTab : function(index){
		// get the tabitem
		var tabItem = this.tabItens[index];
		
		// check the integrity
		if(!__valid(tabItem)){
			syslib.messages.error("syslib.ui.TabPanel.removeTab(): Invalid tab item!");
			return false; 
		}
				
		// remove the selector element
		this.__refs.head.removeChild(tabItem.selector);
		
		// remove the content element
		this.__refs.body.removeChild(tabItem.content);
		
		// unset the tabitem is this tabpanel
		this.tabItens[index] = false;
		
		// check if the tabitem is selected
		if(tabItem.selected){
			this.focusFirstTab();
		}
	}, 

	focusTab : function(index){
		// check if the index is valid
		if(!__valid(this.tabItens[index])){
			return false;
		}
		
		// search the tabitens
		for(var i in this.tabItens){
			// get the tabitem
			var tabItem = this.tabItens[i];
			
			// check the integrity
			if(!__valid(tabItem) || typeof tabItem != "object"){
				continue; 
			}
			
			// get the content
			var content	= tabItem.content;

			// check the data integrity
			if(!__valid(content)){
				continue;
			}

			// check if this tab is the tab that must be selected
			if(i == index){
				// check if there's a page to load
				if(__valid(tabItem.page)){
					// clear the container
					syslib.element.update(content, '');
					
					// create a new ajax request
					new syslib.ajax.request(tabItem.page, {
						destiny : content,
						store : false
					});
				}
				
				// show the content
				syslib.element.show(content);
			} else {
				// hide the content
				syslib.element.hide(content);
				
				// set the tab as unselected
				tabItem.blurTab();
			}
			
		}
	}, 
	
	focusFirstTab : function(){
		// search the tabitens
		for(var i in this.tabItens){
			// get the tabitem
			var tabItem = this.tabItens[i];
			
			// check the integrity
			if(!__valid(tabItem)){
				syslib.messages.warning("syslib.ui.TabPanel.focusFirstTab(): Invalid tab item!");
				continue; 
			}
			
			// select the tabitem
			tabItem.focusTab();
			
			// return
			return true;
		}
	}
});

/**
 * Object that store all created tabs
 * @type Object<syslib.ui.TabPanel>
 * @lends syslib.ui.TabPanel
 */
syslib.ui.TabPanel.registeredTabPanels = {};

/**
 * Focus one tab in the specified tabPanel 
 * @param String tabPanelID The id of the tabpanel
 * @param int tabIndex The index of the tab in the tabpanel
 */
syslib.ui.TabPanel.focusTab = function(tabPanelID, tabIndex){
	// try to obtain the specified tabpanel
	var tabPanel = syslib.ui.TabPanel.registeredTabPanels[tabPanelID];
	
	// check the tabpanel integrity
	if(!__valid(tabPanel)){
		syslib.messages.error("syslib.ui.TabPanel.focusTab(): The specified tabpanel ["+tabPanel+"] is invalid!");
	}
	
	// get the tabitem
	var tabItem = tabPanel.tabItens[tabIndex];
	
	// check the integrity of the tab
	if(!__valid(tabPanel)){
		syslib.messages.error("syslib.ui.TabPanel.focusTab(): The specified tab index ["+tabIndex+"] is invalid!");
	}
	
	// focus the tab
	tabItem.focusTab();
};

/**
 * 
 * @param String tabPanelID The ID of the tabpanel
 * @param String label The label of the tab 
 * @param String  htmlContent The content of the tab
 * @param String page A page to be requested via AJAX 
 */
syslib.ui.TabPanel.addCustomTab = function(tabPanelID, label, htmlContent, page, closeable){
	// try to obtain the specified tabpanel
	var tabPanel = syslib.ui.TabPanel.registeredTabPanels[tabPanelID];
	
	// check the tabpanel integrity
	if(!__valid(tabPanel)){
		syslib.messages.error("syslib.ui.TabPanel.focusTab(): The specified tabpanel ["+tabPanel+"] is invalid!");
		return false;
	}
	
	try{
		// try to create a new tag
		var tab = new syslib.ui.TabItem(tabPanel, label, htmlContent);
	} catch(e){
		syslib.messages.error("syslib.ui.TabPanel.createCustomTab(): Error creating the custom tab: " + e.message);
		return false;
	}
	
	// set the page, if needed
	if(__valid(page)){
		tab.page = page;
	}
	
	// set as not closeable, if needed
	if(typeof closeable != undefined){
		tab.closeable = closeable;
	} else {
		tab.closeable = true;
	}
	
	// create the tab
	tab.create();
	
	// add to the tabpanel
	var index = tabPanel.addTab(tab);
	
	// focus the tab
	syslib.ui.TabPanel.focusTab(tabPanelID, index);
	
	// return the index
	return index;
};